package com.example.hbutliving.decorate.question;

import com.example.hbutliving.config.qiniu.QiNiuConfig;
import com.example.hbutliving.domain.dao.question.Question;
import com.example.hbutliving.domain.dao.User;
import com.example.hbutliving.domain.dto.Publisher;
import com.example.hbutliving.domain.result.MyAnswerResult;
import com.example.hbutliving.domain.result.QuestionResult;
import com.example.hbutliving.mapper.question.QuestionDao;
import com.example.hbutliving.mapper.UserDao;
import com.example.hbutliving.util.UserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
@Component
public class AnswerDecorate {
    @Autowired
    private QuestionDao questionDao;

    @Autowired
    private UserDao userDao;
    public void decorateFatherQuestion(List<MyAnswerResult> myAnswerResults) {
        if( myAnswerResults==null||myAnswerResults.size()==0){
            return;
        }
        List<Question> questions = questionDao.getQuestionsByAnswerIds(
                myAnswerResults.stream().mapToInt(MyAnswerResult::getId).toArray(), UserUtil.getUserId());
        List<QuestionResult> questionResults = questions.stream()
                .map(QuestionResult::new)
                .collect(Collectors.toList());
        Map<Integer, QuestionResult> questionMap = questionResults.stream()
                .collect(Collectors.toMap(QuestionResult::getId,questionResult->questionResult));
        for (MyAnswerResult myAnswerResult: myAnswerResults) {
            QuestionResult questionResult = questionMap.get(myAnswerResult.getQuestionId());
            myAnswerResult.setQuestion(questionResult);
        }
    }
    public void decorateMyUserInfo(List<MyAnswerResult> myAnswerResults) {
        if( myAnswerResults==null||myAnswerResults.size()==0){
            return;
        }
        User myInfo = UserUtil.getUser();
        Publisher publisher = userDao.getPublisherById(myInfo.getId());
        publisher.setAvatar(QiNiuConfig.URL+publisher.getAvatar());
        for (MyAnswerResult myAnswerResult: myAnswerResults) {
            myAnswerResult.setPublisher(publisher);
        }
    }
}
